package a66weding.com.jiehuntong.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;

import android.view.View;

import a66weding.com.jiehuntong.R;
import a66weding.com.jiehuntong.base.BaseActivity;
import a66weding.com.jiehuntong.fragment.BaseFragment;


/**
 * 充当容器的activity
 */
public class ContainerActivity<T> extends BaseActivity {

    public static final String EXTRA_FRAGMENT_CLASS_NAME = "class_name";
    public static final String EXTRA_STRING_TITLE = "title";
    public static final String EXTRA_BOOLEAN_NEED_CHANGE_TITLE = "is_need_change_title";
    private BaseFragment mFrg;

    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        Bundle bundle = getIntent().getExtras();
        String className = bundle.getString(EXTRA_FRAGMENT_CLASS_NAME);
        String title = bundle.getString(EXTRA_STRING_TITLE);
        boolean isNeedChangeTitle = bundle.getBoolean(EXTRA_BOOLEAN_NEED_CHANGE_TITLE);

            setContentView(R.layout.act_container);
        if (title != null)
        try {
            Class<?> fragmentC = Class.forName(className);
            mFrg = (BaseFragment) fragmentC.newInstance();
            mFrg.setArguments(bundle);
        } catch (ClassNotFoundException e) {

            e.printStackTrace();
        } catch (InstantiationException e) {

            e.printStackTrace();
        } catch (IllegalAccessException e) {

            e.printStackTrace();
        }
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.container_fg, mFrg);
        transaction.setTransition(FragmentTransaction.TRANSIT_NONE);
        transaction.commit();

    }

    @Override
    protected View getContentView() {
        return null;
    }

    /**
     * 打开一个装有某fragment的activity
     *
     * @param context
     * @param frgClass
     */
    public static void startActivity(Context context, Class<? extends BaseFragment> frgClass) {
        startActivity(context, frgClass, null, null, true);
    }

    public static void startActivity(Context context, Class<? extends BaseFragment> frgClass, Bundle extras) {
        startActivity(context, frgClass, extras, null, true);
    }

    public static void startActivity(Context context, Class<? extends BaseFragment> frgClass, Bundle extras,
                                     String title) {
        startActivity(context, frgClass, extras, title, true);
    }

    /**
     * 打开容器activity
     *
     * @param context
     * @param frgClass
     * @param extras
     * @param title
     */
    public static void startActivity(Context context, Class<? extends BaseFragment> frgClass, Bundle extras,
                                     String title, boolean needChangeTitle) {
        Intent i = new Intent(context, ContainerActivity.class);
        i.putExtra(EXTRA_FRAGMENT_CLASS_NAME, frgClass.getName());

        if (extras != null)
            i.putExtras(extras);

        if (title != null) {
            i.putExtra(EXTRA_STRING_TITLE, title);
        }
        if (needChangeTitle) {
            i.putExtra(EXTRA_BOOLEAN_NEED_CHANGE_TITLE, needChangeTitle);
        }
        context.startActivity(i);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }
}
